package thread2;

import java.util.concurrent.*;

public class Test9 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for (int i = 1; i <= 1000; i++) {
                    sum += 1;
                }
                return sum;
            }
        };
        FutureTask<Integer> task = new FutureTask<>(callable);
        Thread t = new Thread(task);
        t.start();

        //如果当前这里的callable任务已经执行完了~, get就会立即获取到结果,并返回~
        //如果当前这里的callable还没执行完, get就会阻塞等待.当这里的get 执行完毕之后,才能够得到这里的结果.
        Integer result = task.get();
        System.out.println(result);
    }
}
